package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.RoleSettingService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: bowen
 * @Date: 2019/8/5 10:52
 * @Description:
 */
@RestController
@RequestMapping("/system/role")
public class RoleSettingController {

	@Reference
	RoleSettingService roleSettingService;
	/**
	 *  @描述：增加菜单项
	 */
	@RequestMapping("/addRole")
	public Result addRole(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds) {
		try {
			roleSettingService.addRole(role,permissionIds,menuIds);
			return new Result(true,"增加角色成功");
		} catch(Exception e){
			e.printStackTrace();
			return new Result(false, "增加角色失败");
		}

	}
	/**
	 *  @描述：增加菜单项
	 */
	@RequestMapping("/edit")
	public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds) {
		try {
			roleSettingService.edit(role,permissionIds,menuIds);
			return new Result(true,"编辑角色成功");
		} catch(Exception e){
			e.printStackTrace();
			return new Result(false, "编辑角色失败");
		}

	}
	/**
	 *  @描述：分页查询
	 */
	@RequestMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
		return roleSettingService.findPage(queryPageBean.getCurrentPage(),
				queryPageBean.getPageSize(),
				queryPageBean.getQueryString());
	}

	/**
	 *  @描述：根据id查询基本信息
	 */
	@RequestMapping("/findRoleByID")
	public Result findRoleByID(Integer id) {
		try {
			Role role =  roleSettingService.findRoleByID(id);
			return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,role);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);

		}
	}
	/**
	 *  @描述：查询所有角色
	 */
	@RequestMapping("/findAll")
	public Result findAll() {
		try {
			List<Role> roles =  roleSettingService.findAll();
			return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,roles);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);

		}
	}

	/**
	 *  @描述：根据id查询基本信息
	 */
	@RequestMapping("/delete")
	public Result delete(@RequestBody Integer id) {
		try {
			roleSettingService.delete(id);
			return new Result(true, "删除成功");
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false, "删除失败");

		}
	}
	/**
	 *  @描述：根据检查组id查询关联信息
	 */
	@RequestMapping("/findMenuById")
	public Result findRelationsById(Integer id) {
		try {
			Integer[] ids = roleSettingService.findMenuById(id);
			return new Result(true,"菜单查询成功",ids);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,"菜单查询失败");
		}
	}
	/**
	 *  @描述：根据检查组id查询关联信息
	 */
	@RequestMapping("/findPermissionById")
	public Result findPermissionById(Integer id) {
		try {
			Integer[] ids = roleSettingService.findPermissionById(id);
			return new Result(true,"权限查询成功",ids);
//			return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,ids);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,"权限查询失败");
//			return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
		}
	}
}
